package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnMultiListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.DirectPushAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.DirectPushBean;
import com.shenzhen.horselive.databinding.ActivityDirectPushDetailsBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

public class DirectPushDetailsActivity extends AppBarActivity {

    ActivityDirectPushDetailsBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =  ActivityDirectPushDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("直推会员明细 ");
        initviews();
        initData(0);

    }

    private void initData(int  page) {
        String  id = getIntent().getStringExtra("id");
        if (id==null)
            return;

        ApiFactory.getApiService().directPushDetails(User.get().getMyInfo().getToken(),id,
                "20",page+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<DirectPushBean>(this) {
                    @Override
                    protected void onNextInActive(DirectPushBean directPushBean) {
                        if (binding.smartRefresh.isRefreshing())
                        {
                            binding.smartRefresh.finishRefresh();
                        }
                        if (binding.smartRefresh.isLoading())
                        {
                            binding.smartRefresh.finishLoadMore();
                        }

                        if (directPushBean.isSuccess())
                        {
                            if (page==0)
                            {
                                adapter.setNewInstance(directPushBean.getData());
                            }
                            else
                            {
                                if (directPushBean.getData()!=null&&directPushBean.getData().size()>0)
                                adapter.addData(directPushBean.getData());
                                else
                                {
                                    binding.smartRefresh.finishLoadMoreWithNoMoreData();
                                }
                            }

                        }else
                        {
                            showToast(directPushBean.getErrMessage());
                        }
                    }
                });


    }

    DirectPushAdapter  adapter;
    private   int  nowpage;
    private void initviews() {
        binding.smartRefresh.setEnableRefresh(false);
        binding.smartRefresh.setEnableRefresh(true);
        adapter= new DirectPushAdapter(R.layout.item_direct_push);
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerview.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.img_call);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId())
                {
                    case R.id.img_call:
                       String  phone = DirectPushDetailsActivity.this.adapter.getItem(position).getPhone();
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        break;

                }
            }
        });
    binding.smartRefresh.setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            nowpage=0;
            initData(nowpage);
        }
    });
   binding.smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
       @Override
       public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
           initData(++nowpage);
       }
   });
    }

    public static void start(Context context,String  grade) {
        Intent starter = new Intent(context, DirectPushDetailsActivity.class);
        starter.putExtra("id",grade);
        context.startActivity(starter);
    }


}